import store from "~/store"


function check_group_id(group_id) {
    let group = [
        {
            group_id: '2',
            name: 'person',
        },
        {
            group_id: '3',
            name: 'company_admin',
        },
        {
            group_id: '1',
            name: 'super_admin',
        },
        {
            group_id: '4',
            name: 'staff',
        },
    ]
    let find = group.find(item => item.group_id == group_id)
    if (find) {

        return find.name
    }
    return 'null'
}

function hasPermission(value, el = false) {
    if (!Array.isArray(value)) {
        throw new Error(`需要配置权限，例如 v-permission="['getStatistics3,GET']"`)
    }
    let hasAuth = check_group_id(store.state.user.group) == 'super_admin'
    if (!hasAuth) {
        hasAuth = check_group_id(store.state.user.group) == 'company_admin'
    }

    if (el && !hasAuth) {
        el.parentNode && el.parentNode.removeChild(el)
    }
    return hasAuth
}

// function hasPermission(value, el = false) {
//     if (!Array.isArray(value)) {
//         throw new Error(`需要配置权限，例如 v-permission="['getStatistics3,GET']"`)
//     }
//     const hasAuth = value.findIndex(v => store.state.permissions.includes(v)) != -1 


//     if (el && !hasAuth) {
//         el.parentNode && el.parentNode.removeChild(el)
//     }
//     return hasAuth
//     return true
// }

export default {
    install(app) {
        app.directive("permission", {
            mounted(el, binding) {
                hasPermission(binding.value, el)
            }
        })
    }
}